﻿using System.Linq;
using Framework;
using ISSMoviePlex.DAL.Models;
using System.Collections.Generic;

namespace ISSMoviePlex.DAL.Repositories
{
    public class RoomRepository : BaseRepository<int, Room, RoomRepository>
    {
        private readonly ScheduleRepository _scheduleRepository = ScheduleRepository.Instance;
        private readonly MovieRepository _movieRepository = MovieRepository.Instance;

        public override void ResolveDependencies()
        {
            _scheduleRepository.IsUnitTest = IsUnitTest;
            _movieRepository.IsUnitTest = IsUnitTest;

            _scheduleRepository.Initialize();
            _movieRepository.Initialize();
        }

        public override Room FindById(int id)
        {
            return Context.Rooms.SingleOrDefault(r => r.RoomId == id);
        }

        public Room FindRoomBySchedule(int scheduleId)
        {
            var schedule = _scheduleRepository.FindById(scheduleId);
            return !Is.IsNull(schedule) ? _scheduleRepository.FindById(scheduleId).Room : null;
        }

        public Room FindRoomByMovieSchedule(int movieId, int scheduleId)
        {
            var schedule = _movieRepository.FindById(movieId).Schedules.SingleOrDefault(r => r.ScheduleId == scheduleId);
            if (schedule != null)
                return schedule.Room;
            return null;
        }
    }
}
